package org.cnc.gui;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.cnc.core.MachineControl.Command;
import org.cnc.machine.CutterPosition;

public class StatusPanel extends JPanel implements ChangeListener {
	
	
	private ChangeListener changeListener;
	
	@Override
	public void stateChanged(ChangeEvent e)
	{
		changeListener.stateChanged(new ChangeEvent(this));
	}
	
	public void addChangeListener( ChangeListener listener )
	{
		changeListener = listener;
	}
	
	private static final long serialVersionUID = 1L;

	public StatusPanel()
	{
		//add(button);
		InitGui();
	}
	
	public void InitGui() 
	{
		txtXCutter = new JTextField();
		txtXCutter.setPreferredSize(new Dimension(50, 20));
		
		txtYCutter = new JTextField();
		txtYCutter.setPreferredSize(new Dimension(50, 20));
		
		txtZCutter = new JTextField();
		txtZCutter.setPreferredSize(new Dimension(50, 20));
				
		JLabel lblXCutter = new JLabel("Cutter(X)");
		lblXCutter.setForeground(Color.LIGHT_GRAY);
		
		JLabel lblYCutter = new JLabel("Cutter(Y)");
		lblYCutter.setForeground(Color.LIGHT_GRAY);
		
		JLabel lblZCutter = new JLabel("Cutter(Z)");
		lblYCutter.setForeground(Color.LIGHT_GRAY);
		JScrollPane scrollingArea = new JScrollPane();
		
		JLabel cutterXYZ = new JLabel("\u041A\u043E\u043E\u0440\u0434\u0438\u043D\u0430\u0442\u044B \u0440\u0435\u0437\u0446\u0430:");
		cutterXYZ.setForeground(Color.LIGHT_GRAY);	
		
		JLabel stack = new JLabel("\u0421\u0442\u0435\u043A \u043A\u043E\u043C\u0430\u043D\u0434:");
		stack.setForeground(Color.LIGHT_GRAY);	
		
		JLabel info = new JLabel("<A> - автомат, <Р> - ручной, <Ш> - шаг");
		info.setHorizontalAlignment(SwingConstants.CENTER);

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(55)
							.addComponent(cutterXYZ))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(78)
							.addComponent(stack))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(25)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblXCutter)
									.addGap(18)
									.addComponent(txtXCutter, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblZCutter)
									.addGap(18)
									.addComponent(txtYCutter, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(lblYCutter)
									.addGap(18)
									.addComponent(txtZCutter, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)))
							.addPreferredGap(ComponentPlacement.RELATED, 102, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(info, GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))
						.addComponent(scrollingArea, GroupLayout.PREFERRED_SIZE, 223, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(23)
					.addComponent(cutterXYZ)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(lblXCutter))
						.addComponent(txtXCutter, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(1)
							.addComponent(lblZCutter))
						.addComponent(txtYCutter, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(14)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblYCutter)
						.addComponent(txtZCutter, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(15)
					.addComponent(stack)
					.addGap(18)
					.addComponent(scrollingArea, GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(info)
					.addContainerGap())
		);
		
		listData = new DefaultListModel();
		JList commandList = new JList(listData);
		scrollingArea.setViewportView(commandList);
		groupLayout.setAutoCreateGaps(true);
		groupLayout.setAutoCreateContainerGaps(true);
		
		setLayout(groupLayout);
	}
	
	public void updateStatus(Command c, CutterPosition cutterPosition) 
	{
		listData.insertElementAt(c.toString(), 0);
		
		txtXCutter.setText(" " + cutterPosition.x() );
		txtYCutter.setText(" " + cutterPosition.y() );
		txtZCutter.setText(" " + cutterPosition.z() );		
	}
	
	// fields
	private JTextField txtXCutter;
	private JTextField txtYCutter;
	private JTextField txtZCutter;
	private DefaultListModel listData;
}
